<template>
  <a-modal
    title="新增机构"
    :width="900"
    :visible="visible"
    :confirm-loading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="formLoading">
      <a-form :form="form">
        <a-form-item
          label="机构名称"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          has-feedback
        >
          <a-input
            v-decorator="['name', {rules: [{required: true, message: '请输入机构名称！'}]}]"
            placeholder="请输入机构名称"
          />
        </a-form-item>

        <a-form-item
          label="唯一编码"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          has-feedback
        >
          <a-input
            v-decorator="['code', {rules: [{required: true, message: '请输入唯一编码！'}]}]"
            placeholder="请输入唯一编码"
          />
        </a-form-item>

        <a-form-item
          label="上级机构"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          has-feedback
        >
          <a-tree-select
            v-decorator="['pid', {rules: [{ required: true, message: '请选择上级机构！' }]}]"
            style="width: 100%"
            :dropdown-style="{ maxHeight: '300px', overflow: 'auto' }"
            :tree-data="orgTree"
            placeholder="请选择上级机构"
            tree-default-expand-all
          >
            <span
              slot="title"
              slot-scope="{ id }"
            >{{ id }}
            </span>
          </a-tree-select>
        </a-form-item>

        <a-form-item
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          label="排序"
        >
          <a-input-number
            v-decorator="['sort', { initialValue: 100 }]"
            placeholder="请输入排序"
            style="width: 100%"
            :min="1"
            :max="1000"
          />
        </a-form-item>

        <a-form-item
          label="备注"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
          has-feedback
        >
          <a-textarea
            v-decorator="['remark']"
            :rows="4"
            placeholder="请输入备注"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>


<script>
  import { sysOrgAdd ,getOrgTree} from '@/api/modular/system/orgManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        orgTree:[],
        visible: false,
        confirmLoading: false,
        formLoading:true,
        form: this.$form.createForm(this)
      }


    },
    methods: {
      //初始化方法
      add () {
        this.visible = true
        this.getOrgTree()
      },

      /**
       * 获取机构树，并加载于表单中
       */
      getOrgTree(){
        getOrgTree().then((res) => {
          this.formLoading = false
          if(!res.success){
            this.orgTree=[]
            return
          }
          this.orgTree=[{
            "id": "-1",
            "parentId": "0",
            "title": "顶级",
            "value": "0",
            "pid": "0",
            "children":res.data
          }]
        })
      },

      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            sysOrgAdd(values).then((res) => {
              if(res.success){
                this.$message.success('新增成功')
                this.visible = false
                this.confirmLoading = false
                this.$emit('ok', values)
                this.form.resetFields();
              }else{
                this.$message.error('新增失败：'+res.message)
              }
            }).finally((res) =>{
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields();
        this.visible = false
      }
    }
  }
</script>
